import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/archives/app_bar.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/archives/archives_person.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/archives/archives_role.dart';
import 'package:flutter_eve_plugin/constants/constant.dart';
import 'package:flutter_eve_plugin/pages/main/actions.dart';
import 'package:flutter_eve_plugin/plugin/main.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/utils/image_util.dart';
import 'package:flutter_redux/flutter_redux.dart';

class ArchivesMainPage extends StatefulWidget {
  final String title;
  ArchivesMainPage({Key key, this.title}) : super(key: key);

  _ArchivesMainPageState createState() => _ArchivesMainPageState();
}

class _ArchivesMainPageState extends State<ArchivesMainPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  int _index = 0;

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: 2);
    _tabController.addListener(() {
      setState(() {
        _index = _tabController.index;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(
      onInit: (store) {
        PluginEve.sendMessage(PluginConstant.httpUserInfo, httpType: HttpType.POST)
            .then((data) => store.dispatch(userInfo(store, data)));
      },
        converter: (store) {
      return AppStateViewModel(state: store.state);
    }, builder: (context, vm) {
      int coin = vm.state.accountInfoState?.userInfoModel?.gloryCoinCount ?? 0;
      print('glorycoin $coin');
      return Container(
        decoration: BoxDecoration(
            image: DecorationImage(
                image:
                    AssetImage(imageUtilYeXiu('archives/icon_archives_bg@3x')),
                fit: BoxFit.fitWidth,
                alignment: Alignment.topCenter)),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: buildArchivesAppBar(_index, '角色', '个人', coin,(index) {
            setState(() {
              _index = index;
              _tabController.animateTo(_index);
            });
          }),
          body: TabBarView(
            controller: _tabController,
            children: <Widget>[ArchivesRoleWidget(), ArchivesPersonWidget()],
          ),
        ),
      );
    });
  }
}
